//
//  XYCollectionViewCell.m
//  ZhaoQiPei
//
//  Created by baiye on 2017/2/5.
//  Copyright © 2017年 XiangYun. All rights reserved.
//

#import "XYCollectionView.h"

@interface XYCollectionView ()

@property (nonatomic, weak) NSLayoutConstraint * heightConstraint;

@end

@implementation XYCollectionView

- (void)reloadData {
    [super reloadData];
    
    if (self.automaticallyAdjustsHeight) {
        [self adjustsHeight];
    }
}

- (void)adjustsHeight {
    if (self.heightConstraint) {
        self.heightConstraint.constant = self.collectionViewLayout.collectionViewContentSize.height;
    }
    else {
        [self removeConstraints:self.constraints];
        NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.collectionViewLayout.collectionViewContentSize.height];
        [self addConstraint:constraint];
        self.heightConstraint = constraint;
    }
}

@end
